/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
#![allow(
    dead_code,
    mutable_transmutes,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]
use crate::common::*;
extern "C" {
    fn strdup(_: *const libc::c_char) -> *mut libc::c_char;
    fn strchr(_: *const libc::c_char, _: libc::c_int) -> *mut libc::c_char;
    fn sudo_fatalx_nodebug_v1(fmt: *const libc::c_char, _: ...) -> !;
    fn sudo_debug_printf2_v1(
        func: *const libc::c_char,
        file: *const libc::c_char,
        line: libc::c_int,
        level: libc::c_int,
        fmt: *const libc::c_char,
        _: ...
    );
    static mut sudoers_subsystem_ids: [libc::c_uint; 0];
    static mut sudo_user: sudo_user;
    fn sudo_gethostname_v1() -> *mut libc::c_char;
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct gid_list {
    pub ngids: libc::c_int,
    pub gids: *mut gid_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct sudo_user {
    pub pw: *mut passwd,
    pub _runas_pw: *mut passwd,
    pub _runas_gr: *mut group,
    pub cmnd_stat: *mut stat,
    pub name: *mut libc::c_char,
    pub path: *mut libc::c_char,
    pub tty: *mut libc::c_char,
    pub ttypath: *mut libc::c_char,
    pub host: *mut libc::c_char,
    pub shost: *mut libc::c_char,
    pub runhost: *mut libc::c_char,
    pub srunhost: *mut libc::c_char,
    pub prompt: *mut libc::c_char,
    pub cmnd: *mut libc::c_char,
    pub cmnd_args: *mut libc::c_char,
    pub cmnd_base: *mut libc::c_char,
    pub cmnd_safe: *mut libc::c_char,
    pub class_name: *mut libc::c_char,
    pub krb5_ccname: *mut libc::c_char,
    pub gid_list: *mut gid_list,
    pub env_vars: *const *mut libc::c_char,
    pub role: *mut libc::c_char,
    pub type_0: *mut libc::c_char,
    pub cwd: *const libc::c_char,
    pub iolog_file: *mut libc::c_char,
    pub gids: *mut gid_t,
    pub execfd: libc::c_int,
    pub ngids: libc::c_int,
    pub closefrom: libc::c_int,
    pub lines: libc::c_int,
    pub cols: libc::c_int,
    pub flags: libc::c_int,
    pub max_groups: libc::c_int,
    pub timeout: libc::c_int,
    pub umask: mode_t,
    pub uid: uid_t,
    pub gid: uid_t,
    pub sid: pid_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union sudo_in_addr_un {
    pub ip4: in_addr,
    pub ip6: in6_addr,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct interface {
    pub entries: C2RustUnnamed_0,
    pub family: libc::c_uint,
    pub addr: sudo_in_addr_un,
    pub netmask: sudo_in_addr_un,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub sle_next: *mut interface,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct interface_list {
    pub slh_first: *mut interface,
}
#[no_mangle]
pub fn init_envtables() -> bool {
    true
}
#[no_mangle]
pub fn user_is_exempt() -> bool {
    false
}
#[no_mangle]
pub fn sudo_setspent() {}
#[no_mangle]
pub fn sudo_endspent() {}

#[no_mangle]
pub fn group_plugin_query(
    mut user: *const libc::c_char,
    mut group: *const libc::c_char,
    mut pw: *const passwd,
) -> libc::c_int {
    false as libc::c_int
}

#[no_mangle]
pub fn get_interfaces() -> *mut interface_list {
    unsafe {
        static mut dummy: interface_list = {
            interface_list {
                slh_first: 0 as *const interface as *mut interface,
            }
            //init
        };
        &mut dummy
    } //unsafe
}

/*
 * Look up the hostname and set user_host and user_shost.
 */
#[no_mangle]
pub fn get_hostname() {
    let mut cp: *mut libc::c_char = std::ptr::null_mut::<libc::c_char>();
    unsafe {
        debug_decl!(SUDOERS_DEBUG_UTIL!());

        sudo_user.host = sudo_gethostname_v1();
        if !(sudo_user.host).is_null() {
            cp = strchr(sudo_user.host, '.' as i32);
            if !cp.is_null() {
                *cp = '\0' as i32 as libc::c_char;
                sudo_user.shost = strdup(sudo_user.host);
                if (sudo_user.shost).is_null() {
                    sudo_fatalx!(
                        b"%s: %s\0" as *const u8 as *const libc::c_char,
                        get_function_name!(),
                        b"unable to allocate memory\0" as *const u8 as *const libc::c_char
                    );
                }

                *cp = '.' as i32 as libc::c_char;
            } else {
                sudo_user.shost = sudo_user.host;
            }
        } else {
            sudo_user.shost = strdup(b"localhost\0" as *const u8 as *const libc::c_char);
            sudo_user.host = sudo_user.shost;
            if (sudo_user.host).is_null() {
                sudo_fatalx!(
                    b"%s: %s\0" as *const u8 as *const libc::c_char,
                    get_function_name!(),
                    b"unable to allocate memory\0" as *const u8 as *const libc::c_char
                );
            }
        }
        sudo_user.runhost = sudo_user.host;
        sudo_user.srunhost = sudo_user.shost;
        debug_return!();
    } //unsafe
}
